package com.company.structural.proxy_pattern.client;

import com.company.structural.proxy_pattern.dynamic_proxy.*;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @ClassName Client1
 * @Author tiezheng.han
 * @Date 2020/9/1 19:53
 **/
public class Client1 {
    public static void main(String[] args) {
        InvocationHandler handler = null;
        AbstractUserDAO userDAO = new UserDAO();
        handler = new DAOLogHandler(userDAO);
        AbstractUserDAO proxy = null;

        // 动态代理创建代理对象，用于代理一个AbsractUserDAO类型的真是主题对象
        proxy = (AbstractUserDAO) Proxy.newProxyInstance(AbstractUserDAO.class.getClassLoader(),new Class[]{AbstractUserDAO.class},handler);
//        proxy = new UserDAO();
        // 调用代理对象的业务方法
        proxy.findUserById("张无忌");

        System.out.println("-----------------------------------------");

        AbstractDocumentDAO docDAO = new DocumentDAO();
        handler = new DAOLogHandler(docDAO);
        AbstractDocumentDAO proxy_new = null;

        // 动态创建代理对象，用于代理一个AbstractDocumentDAO 类型的真实主题对象
        proxy_new = (AbstractDocumentDAO) Proxy.newProxyInstance(AbstractDocumentDAO.class.getClassLoader(),new Class[]{AbstractDocumentDAO.class},handler);
        // 调用代理对象的业务方法
        proxy_new.deleteDocumentById("D001");
    }
}
